{extend name="adminblue/base" /} 
{block name="resources"/}
{/block}
{block name="alert_info"}
<p>1.如果要使用伪静态，需找到application/config.php中的“REWRITE_MODEL”，将false改为true。</p>
<p style="margin: 0;">2.路由规则添加成功后，删除掉runtime下的缓存文件，再清除掉浏览器缓存，最后进入前台首页。</p>
{/block}
{block name="main"}
<div class="set-style">
	<dl>
		<dt><span class="required">*</span>伪静态路由规则：</dt>
		<dd>
			<input type="text" class="input-common" id="rule" onchange="url_route_if_exists('rule',this);" onKeyUp="value=value.replace(/[^\d|\w]/g,'')" value="{$routeDetail.rule}" maxlength="100" onpaste="return false"/>
			<p class="hint"></p>
			<p class="error rule_error">该路由规则已经存在</p>
		</dd>
	</dl>
	<dl>
		<dt><span class="required">*</span>路由地址：</dt>
		<dd>
			<input type="text" class="input-common" id="route" onchange="url_route_if_exists('route',this);" value="{$routeDetail.route}"  maxlength="150" />
			<p class="hint">路由地址由<i class="important-note">模块名/控制器名/方法名</i>组成，如shop/goods/goodslist</p>
			<p class="error route_error">该路由地址已经存在</p>
		</dd>
	</dl>
	<dl>
		<dt>所属模块：</dt>
		<dd>
			<select id="route_model" class="select-common">
				<option value="1" {if condition="$routeDetail['route_model'] eq 1"}selected{/if}>shop</option>
				<!-- <option value="2" {if condition="$routeDetail['route_model'] eq 2"}selected{/if}>wap</option>
				<option value="3" {if condition="$routeDetail['route_model'] eq 3"}selected{/if}>admin</option> -->
			</select>
		</dd>
	</dl>
	<dl>
		<dt>是否启用：</dt>
		<dd>
			<p><input type="checkbox" id="is_open" class="checkbox" {if condition="$routeDetail['is_open'] eq 1"}checked{/if}></p>
		</dd>
	</dl>
	<dl>
		<dt>描述：</dt>
		<dd>
			<textarea name="" id="remark" class="textarea-common">{$routeDetail.remark}</textarea>
		</dd>
	</dl>	
	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="save();">保存</button>
			<button class="btn-common-cancle btn-big" onclick="window.location.href = window.history.go(-1);" >返回</button>
		</dd>
	</dl>
	<input type="hidden" id="routeid" value="{$routeDetail.routeid}">
</div>
<script>
//关键字
var keyword = new Array("addons","base","cms","components","helpcenter","login","member","notice","order","topic","basecontroller");
$(function(){
	keywordstr = keyword.join(",");
	$("#rule").next().html('伪静态路由规则不可使用<i class="important-note">"' + keywordstr +'"</i>这些关键字');
})
var rule_if_exists = false; //伪静态路由规则是否存在
var route_if_exists = false; //路由地址是否存在
//判断路由规则或者路由地址是否存在
function url_route_if_exists(type, event){
	var value = $(event).val().toLowerCase();
	$(event).val(value);
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/config/url_route_if_exists')}",
		async : false,
		data : {
			"type" : type,
			"value" : value
		},
		success : function(data){
			if(data == true && type == "rule"){
				rule_if_exists = true;
				$(".rule_error").text("该路由规则已经存在").show();
			}else if(data == false && type == "rule"){
				$(".rule_error").hide();
				rule_if_exists = false;
			}
			if(data == true && type == "route"){
				route_if_exists = true;
				$(".route_error").text("该路由地址已经存在").show();
			}else if(data == false && type == "route"){
				$(".route_error").hide();
				route_if_exists = false;
			}
		}
	})
}
var is_sub = false;
function save(){
	var rule = $("#rule").val().toLowerCase();
	var route = $("#route").val().toLowerCase();
	var route_model = $("#route_model").val();
	if($("#is_open").is(":checked")){
		var is_open = 1;
	}else{
		var is_open = 0;
	}
	var remark = $("#remark").val();
	var routeid = $("#routeid").val();
	if(vertify(rule,route)){
		if(!is_sub){
			is_sub = true;
			$.ajax({
				type : "post",
				url : "{:__URL('ADMIN_MAIN/config/updateRoutingRule')}",
				data : {
					"routeid" : routeid,
					"rule" : rule,
					"route" : route,
					"route_model" : route_model,
					"is_open" : is_open,
					"remark" : remark
				},
				success : function(data){
					is_sub = false;
					if(data["code"] > 0){
						showTip("添加成功","success");
						location.href = "{:__URL('ADMIN_MAIN/config/customPseudoStaticRule')}";
					}else{
						showTip("添加失败","error");
					}
				}
			})
		}
	}
}

function vertify(rule,route){
	if(rule.length == 0){
		$(".rule_error").text("伪静态路由规则不可为空").show();
		$("#rule").focus();
		return false;
	}else if(rule_if_exists){
		$(".rule_error").text("该路由规则已经存在").show();
		$("#rule").focus();
		return false;
	}else{
		for(var i = 0; i < keyword.length; i++){
			if(rule == keyword[i]){
				$(".rule_error").text("关键字"+ keyword[i]+"不可作为路由规则").show();
				$("#rule").focus();
				return false;
			}
		}
	}
	if(route.length == 0){
		$(".route_error").text("路由地址不可为空").show();
		$("#route").focus();
		return false;
	}else if(route_if_exists){
		$(".route_error").text("该路由地址已经存在").show();
		$("#route").focus();
		return false;
	}
	$(".rule_error").hide();
	$(".route_error").hide();
	return true;
}
</script>
{/block}